<?php

namespace app\admin\model;

use think\Model;

class Task extends Model
{
    // 表名
    protected $name = 'task';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    
    // 追加属性
    protected $append = [
        'callback_type_text',
        'state_text'
    ];
    

    
    public function getCallbackTypeList()
    {
        return ['transfer' => __('Callback_type transfer'),'ivr' => __('Callback_type ivr'),'ivr_transfer' => __('Callback_type ivr_transfer')];
    }     

    public function getStateList()
    {
        return ['0' => __('State 0'),'1' => __('State 1'),'2' => __('State 2')];
    }     


    public function getCallbackTypeTextAttr($value, $data)
    {        
        $value = $value ? $value : $data['callback_type'];
        $list = $this->getCallbackTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getStateTextAttr($value, $data)
    {        
        $value = $value ? $value : $data['state'];
        $list = $this->getStateList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function admin()
    {
        return $this->belongsTo('Admin', 'admin_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function sound()
    {
        return $this->belongsTo('Sound', 'sound_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function speech()
    {
        return $this->belongsTo('Speech', 'speech_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function gateways()
    {
        return $this->belongsToMany('Gateway', 'task_gateway_access', 'gateway_id', 'tid');
    }

    public function admins()
    {
        return $this->belongsToMany('Admin', 'task_admin_access', 'admin_id', 'tid');
    }

    public function taskGatewayAccess()
    {
        return $this->belongsTo('TaskGatewayAccess', 'id', 'tid', [], 'LEFT')->setEagerlyType(0);
    }

}
